//
//  voicessViewController.m
//  XiangMuB
//
//  Created by lanou3g on 16/8/23.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "voicessViewController.h"
#import <iflyMSC/IFlyMSC.h>
#import "PcmPlayer.h"
#import "TTSConfig.h"
#import <AVOSCloud/AVOSCloud.h>
#import "loadShare.h"
@interface voicessViewController ()<IFlySpeechSynthesizerDelegate>//遵循语音合成回调协议

typedef NS_OPTIONS(NSInteger, SynthesizeType) {
    
    NomalType        =5,//普通合成
    urlType          =6,//url合成
    
    
};
@property (strong,nonatomic)UITextView *textView;
//语音合成对象
@property (strong,nonatomic)IFlySpeechSynthesizer *iflySpeechSynthesizer;
//用于播放音频的对象
@property (strong,nonatomic)PcmPlayer *audioPlayer;
//合成方式
@property (assign,nonatomic)SynthesizeType synType;

//解析过程中是否出现错误
@property (assign,nonatomic)BOOL hasError;




@end

@implementation voicessViewController


-(void)viewDidAppear:(BOOL)animated
{
  


}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImageView *image =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    image.image = [UIImage imageNamed:@"fly.jpg"];
    
    [self.view addSubview:image];
    
    //发件人:
    UILabel *lable2  =[[UILabel alloc] initWithFrame:CGRectMake(20, 170, 100 ,20)];
    lable2.text = @"发件人:";
    lable2.backgroundColor = [UIColor clearColor];
    lable2.numberOfLines = 0;
    [self.view addSubview:lable2];
    
    //发件人信息
    UILabel *lable3  =[[UILabel alloc] initWithFrame:CGRectMake(100, 170, 100 ,20)];
    lable3.text = self.sender;
    lable3.backgroundColor = [UIColor clearColor];
    lable3.numberOfLines = 0;
    [self.view addSubview:lable3];
    
    self.textView =[[UITextView alloc] initWithFrame:CGRectMake(10, 200, self.view.frame.size.width-20, 300)];
    
    self.textView.font = [UIFont systemFontOfSize:22];
    
    if (!self.sender) {
        self.textView.text = self.content;

    }
    
    
    self.textView.backgroundColor = [UIColor colorWithRed:192.0f/255.0f  green:192.0f/255.0f  blue:192.0f/255.0f  alpha:0.5];
    [self.view addSubview:self.textView];
    
    
    
    self.view.backgroundColor =[UIColor whiteColor];

    //语音转文字
    UIButton *button =[UIButton buttonWithType:(UIButtonTypeCustom)];
    button.backgroundColor = [UIColor cyanColor];
    button.frame =CGRectMake(100, 100, 100, 50);
    [button addTarget:self action:@selector(Btn:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [button setTag:1000];
    [button setTitle:@"文字转语音" forState:(UIControlStateNormal)];
    
    [self.view addSubview:button];
    
    self.view.backgroundColor =[UIColor cyanColor];
    
    [self initMakeVoice];
}
-(void)Btn:(UIButton *)sender
{
    
    //如果正在播放,那么就停止播放
    if ([self.textView.text isEqualToString:@""]) {
        return;
    }
    if (_audioPlayer != nil && _audioPlayer.isPlaying == YES) {
        
        [_audioPlayer stop];
    }
    
    
    //设置和承诺语音的方式为普通方式
    _synType = NomalType;
    self.hasError = NO;
    //添加代理
    
    _iflySpeechSynthesizer.delegate =self;
    
    //开始播放
    
    
    
    [_iflySpeechSynthesizer startSpeaking:self.textView.text];
}

-(void)initMakeVoice{
    TTSConfig *instance = [TTSConfig sharedInstance];
    
    if (instance == nil) {
        return;
    }
    //合成服务单例
    if(_iflySpeechSynthesizer == nil){
        _iflySpeechSynthesizer = [IFlySpeechSynthesizer sharedInstance];
        
        
    }
    _iflySpeechSynthesizer.delegate = self;
    
    //设置语速:1-100
    [_iflySpeechSynthesizer setParameter:instance.speed forKey:[IFlySpeechConstant SPEED]];
    
    //设置音量:1-100
    [_iflySpeechSynthesizer setParameter:instance.volume forKey:[IFlySpeechConstant VOLUME]];
    //设置音调:1-100
    [_iflySpeechSynthesizer setParameter:instance.pitch forKey:[IFlySpeechConstant PITCH]];
    //设置采样率
    [_iflySpeechSynthesizer setParameter:instance.sampleRate forKey:[IFlySpeechConstant SAMPLE_RATE]];
    
    //设置发音人
    [_iflySpeechSynthesizer setParameter:@" xiaoyan " forKey:[IFlySpeechConstant VOICE_NAME]];
}

-(void)onCompleted:(IFlySpeechError *)error
{
    NSLog(@"错误");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
